import type { ApiResponse } from '@/types/global'
import type {
  GetGameListParamsModel,
  GetBannerParamsModel,
  OutGameParams,
  LoginGameParams,
  LoginChessGameParams,
  GetGameListDataModel,
  GetBannerItemModel,
  LoginOutModel,
} from '@/types/game.types.ts'

const v1 = '/api'
const game = '/game'
const page = '/page'

// 渲染相关
const template = {
  getGameList: (params: GetGameListParamsModel) =>
    useClientApi<ApiResponse<GetGameListDataModel[]>>(v1 + game + '/getGameList', { method: 'post', params }),
  getBanner: (params: GetBannerParamsModel) =>
    useClientApi<ApiResponse<GetBannerItemModel[]>>(v1 + page + '/getBanner', { method: 'get', params }),
  getGameByCompanyCode: (params: LoginChessGameParams) =>
    useClientApi(v1 + game + '/getGameByCompanyCode', { method: 'get', params }),
}

// 登陆相关
const login = {
  login: (params: LoginGameParams) => {
    const obj = {
      platform: 'h5',
      home_url: location.origin,
    }
    params.platform = 'h5'
    params.home_url = location.origin
    params = Object.assign(obj, params)
    return useClientApi<ApiResponse<{ url: string }>>(v1 + game + '/login', { method: 'post', params })
  },
  chessLogin: () => template.getGameByCompanyCode({ company_code: 'SQP' }),
  closeGame: (params: OutGameParams) => useClientApi(v1 + game + '/closeGame', { method: 'post', params }),
  loginOut: (params: OutGameParams) =>
    useClientApi<ApiResponse<LoginOutModel>>(v1 + game + '/loginOut', { method: 'post', params }),
}

const apiBonding = {
  // 退出游戏
  gameOut: (params: OutGameParams) => {
    login.closeGame(params)
    login.loginOut(params)
  },
}

export default {
  ...template,
  ...login,
  ...apiBonding,
}
